﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MessMain
{
    public partial class CreareConf : Form
    {
        Comunication c;
        private string limba;

        public CreareConf(Comunication c, string limba)
        {
            InitializeComponent();
            Rename(limba);

            this.limba = limba;
            this.c = c;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (numeConf.Text.Length == 0)
            {
                MessageBox.Show("Nu ati introdus numele Conferintei!");
            }else
            if (listUsers.SelectedIndex == -1)
            {
                MessageBox.Show("Trebuie sa alegeti un utilizator pentru a crea o conferinta!");
            }
            else if (numeConf.Text.Contains(' '))
            {
                MessageBox.Show("Numele conferintei nu poate contine spatii!");
            }else
            {
                string conf = numeConf.Text;
                TabItems tbi = null;
                c.Invoke((MethodInvoker)delegate
                {
                    tbi = new TabItems(limba);
                    tbi.c = c;
                    tbi.myName = Comunication.myName;
                    for (int i = 0; i < listUsers.Items.Count; i++)
                    {
                        if (listUsers.GetItemChecked(i))
                        {
                            tbi.addUser((string)listUsers.Items[i]);
                        }
                    }
                    tbi.confName = conf;
                    tbi.isConf = true;
                    tbi.updateConf();                 
                    c.tablist.Add(tbi);
                    tbi.sendMsgTo("buna");
                    c.setCtlTab2(tbi);                    
                });
                this.Visible = false;
            }
        }

        private void listUsers_SelectedValueChanged(object sender, MouseEventArgs e)
        {
            if (this.listUsers.SelectedIndex != -1)
            {
                bool r = this.listUsers.GetItemChecked(this.listUsers.SelectedIndex);
                if (r == false)
                {
                    this.listUsers.SetItemChecked(this.listUsers.SelectedIndex, true);
                }
                else
                {
                    this.listUsers.SetItemChecked(this.listUsers.SelectedIndex, false);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void updateList()
        {
            listUsers.Items.Clear();
            for (int i = 0; i < c.users.Items.Count; i++)
            {
                listUsers.Items.Add(c.users.Items[i],false);
            }            
            numeConf.ResetText();
        }

        public void setVisible(bool mode) 
        {
            if (mode == true)
            {
                this.Visible = true;
                updateList();
            }
            else
            {
                this.Visible = false;
            }
        }
    }
}
